﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Reflection;
using MockPrjTeam4.Biz;
using MockPrjTeam4.Entity;
using MockPrjTeam4.Common;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.Globalization;

namespace MockPrjTeam4.Pages.Views
{
    public partial class ListDepartments : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int i = Methods.ConvertNumber(Request.QueryString["ID"]);

            if (chkInactive.Checked)
            {
                gvListDepartments.DataSource = DepartmentsBiz.GetListDepartments();
                gvListDepartments.DataBind();
            }
            else
            {
                gvListDepartments.DataSource = DepartmentsBiz.GetListActiveDepartments();
                gvListDepartments.DataBind();
            }
            
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Forms/AddDepartment.aspx");
        }


        protected void chkInactive_CheckedChanged(object sender, EventArgs e)
        {
                IList<Departments> dep;
                if (chkInactive.Checked)
                {
                    dep = DepartmentsBiz.GetListDepartments();
                }
                else
                {
                    dep = DepartmentsBiz.GetListActiveDepartments();
                }
                gvListDepartments.DataSource = dep;
                gvListDepartments.DataBind();
        }

        protected void Click(object sender, CommandEventArgs e)
        {
            IList<Departments> dep = new List<Departments>();
            string isActive = "";
            if (chkInactive.Checked)
            {
                isActive = "true";
            }
            else
            {
                isActive = "false";
            }
            string range = "";
            switch (e.CommandName)
            {
                case "SearchAll":
                    {
                        dep = DepartmentsBiz.GetListDepartments();
                        break;
                    }
                case "Search09":
                    {
                        //range = "[0-9]";
                        dep = DepartmentsBiz.Search("[0-9]%", isActive);
                        break;
                    }
                case "SearchAE":
                    {
                        Session["range"] = "[A-E]%";
                        range = Convert.ToString(Session["range"].ToString(), CultureInfo.CurrentCulture);
                        dep = DepartmentsBiz.Search(range, isActive);
                        break;
                    }
                case "SearchFJ":
                    {
                        dep = DepartmentsBiz.Search("[F-J]%", isActive);
                        break;
                    }
                case "SearchKN":
                    {
                        dep = DepartmentsBiz.Search("[K-N]%", isActive);
                        break;
                    }
                case "SearchOR":
                    {
                        dep = DepartmentsBiz.Search("[O-R]%", isActive);
                        break;
                    }
                case "SearchSV":
                    {
                        dep = DepartmentsBiz.Search("[S-V]%", isActive);
                        break;
                    }
                case "SearchWZ":
                    {
                        dep = DepartmentsBiz.Search("[W-Z]%", isActive);
                        break;
                    }
            }
            gvListDepartments.DataSource = dep;
            gvListDepartments.DataBind();
        }

        protected void gvListDepartments_PageIndexedChanging(object sender, GridViewPageEventArgs e)
        {
            gvListDepartments.PageIndex = e.NewPageIndex;
            if (chkInactive.Checked)
            {
                gvListDepartments.DataSource = DepartmentsBiz.GetListDepartments();
                gvListDepartments.DataBind();
            }
            else
            {
                gvListDepartments.DataSource = DepartmentsBiz.GetListActiveDepartments();
                gvListDepartments.DataBind();
            }
        }
    }
}